/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.tan.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.room.RoomExtension;
import extension.room.analyse.Card;
import extension.room.tan.TanGame;
import extension.room.tan.TanPlayer;
import extension.room.tan.TanResponse;

import java.util.List;
import log.Debug;

/**
 * @author daz
 */
public class FightCardHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        List<Integer> listCardFights = (List<Integer>) params.getIntArray(TanFlag.FIGHT_CARD);
        RoomExtension ext = (RoomExtension) getParentExtension();
        TanGame game = (TanGame) ext.getGame();
        TanPlayer p = game.getPlayerFight();
        int cardFight = listCardFights.get(0);
        Debug.d(p.getName() + " fight :" + Card.getName(cardFight));

        if (p.getName().equals(user.getName())) {
            if (game.checkCard(cardFight) && !game.isIsFighted()) {
                game.setCurrentCard(cardFight);
                p.removeCardInHand(cardFight);
                List<User> listSend = ext.getParentRoom().getUserList();
                TanResponse response = (TanResponse) ext.getResponse();
                response.sendFightCard(cardFight, 1, game.getGameTurn(), listSend);
                //check remain card
                if (p.getNumberCardInHands() <= 0) {
                    game.doFightAllCard();
                }
                game.nextTurn();

            } else {
                TanResponse response = (TanResponse) ext.getResponse();
                response.sendFightCard(0, user);
            }
        }
    }
}
